from . import *

"""
====================================
公用函数
1:查询当前用户权限集联菜单数据;
2:group字段数据同步修改menu字段数据;
====================================
"""


def user_search_menu_data(user: User, request=None) -> list:
    """
    1:查询当前用户权限集联菜单数据
    :param user: 用户对象
    :param request:
    :return:
    """
    menu1 = Menu.objects.get(pk=1)
    menu2 = Menu.objects.get(pk=2)
    menu3 = Menu.objects.get(pk=3)
    menu4 = Menu.objects.get(pk=4)
    menu5 = Menu.objects.get(pk=5)
    menus = [menu1, menu2, menu3, menu4, menu5]
    childs = user.child_menu.all()
    data = []
    for menu in menus:
        # 临时children存储
        children = []
        for child in childs:
            if child.menu_id == menu.id:
                children.append(
                    ChildMenu.objects.filter(pk=child.id).values()[0])
        if children:
            data2 = Menu.objects.filter(pk=menu.id).values()[0]
            data2.update({"children": children})
            data.append(data2)
    return data


def user_set_menu_data(user: User, group: int, request=None) -> None:
    """
    2:group字段数据同步修改menu字段数据
    :param request:
    :param user: 用户模型对象
    :param group: 用户group字段数据1/2
    :return:
    """
    # 获取当前权限对应menu列表
    menu_group = User.GROUPS_INFO[group]
    # 多对多修改菜单信息
    user.child_menu.set(menu_group)